package com.onelap.bls.tc.activity.calibration;

import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothGatt;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.AlertDialogLayout;
import android.text.InputType;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bls.blslib.base.BaseActivity;
import com.google.gson.Gson;
import com.onelap.bls.tc.R;
import com.onelap.bls.tc.activity.bluetooth_le.BluetoothDialogHelper;
import com.onelap.bls.tc.activity.bluetooth_le.BluetoothLeParams;
import com.onelap.bls.tc.activity.bluetooth_le.BluetoothLeService;
import com.onelap.bls.tc.activity.bluetooth_le.BluetoothLeUtil;
import com.onelap.bls.tc.activity.index.DeviceBean;
import com.onelap.bls.tc.activity.index.IndexActivity;
import com.onelap.bls.tc.utils.BLeUtil;
import com.onelap.bls.tc.utils.TitleBar;

import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.onelap.bls.tc.activity.bluetooth_le.BluetoothLeParams.ServiceParams.EXTRA_DATA;
import static com.onelap.bls.tc.activity.bluetooth_le.BluetoothLeParams.UtilCallbackStatue.STATUS_BluetoothDevice_Get_Data;

@SuppressLint("SetTextI18n")
public class CalibrationActivity extends BaseActivity implements BluetoothLeUtil.BluetoothUtilCallback {
    @BindView(R.id.tv_id)
    TextView tvId;
    @BindView(R.id.tv_version)
    TextView tvVersion;
    @BindView(R.id.tv_electric)
    TextView tvElectric;
    @BindView(R.id.one_et_speed)
    EditText oneEtSpeed;
    @BindView(R.id.one_btn_confirm)
    Button oneBtnConfirm;
    @BindView(R.id.one_tv_show_1)
    TextView oneTvShow1;
    @BindView(R.id.one_tv_show_2)
    TextView oneTvShow2;
    @BindView(R.id.tl_test_one)
    LinearLayout tlTestOne;
    @BindView(R.id.two_et_1)
    EditText twoEt1;
    @BindView(R.id.two_btn_1)
    Button twoBtn1;
    @BindView(R.id.two_iv_1)
    ImageView twoIv1;
    @BindView(R.id.two_et_2)
    EditText twoEt2;
    @BindView(R.id.two_btn_2)
    Button twoBtn2;
    @BindView(R.id.two_iv_2)
    ImageView twoIv2;
    @BindView(R.id.two_et_3)
    EditText twoEt3;
    @BindView(R.id.two_btn_3)
    Button twoBtn3;
    @BindView(R.id.two_iv_3)
    ImageView twoIv3;
    @BindView(R.id.tl_test_two)
    LinearLayout tlTestTwo;
    @BindView(R.id.two_btn_start)
    Button twoBtnStart;
    @BindView(R.id.two_btn_over)
    Button twoBtnOver;
    @BindView(R.id.two_tv_1)
    TextView twoTv1;

    private String calibrationType = "ONE";
    //private BluetoothDevice bluetoothDevice;
    private DeviceBean deviceBean;
   /* private UartService bluetoothLeService;
    private BLeUtil bLeUtil;
    private int intSpeedV1;  //跑步机速度*/

    private boolean isStartPoDu = false;
    private boolean isOverPoDu = false;
    // private int writePoDu = 0;  //跑步机坡度的填写状态
    private BluetoothLeService bluetoothLeService;
    private BluetoothLeUtil bluetoothLeUtil;
    private BluetoothAdapter bluetoothAdapter;

    private ProgressDialog progressDialog;

    private boolean isConnectDevice = false;  //连接到蓝牙设备

    private String tempValue = null;

    private String temp_1 = "";
    private String temp_11 = "";
    private String temp_12 = "";
    private String temp_13 = "";

    private int TIME = 2000;  //每隔1s执行一次.

    private boolean state_1 = true;
    private boolean state_2 = true;
    private boolean state_3 = true;
    private boolean state_4 = true;
    private boolean state_5 = true;

    Handler handler = new Handler();
    private Handler handler2;

    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            try {
                handler.postDelayed(this, TIME);
                bluetoothLeUtil.displayVersionBattery();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };
    private float inputNum;
    private float inputNum2;
    private MaterialDialog.Builder materialDialog = null;
    private AlertDialog alertDialog;

    @SuppressLint("HandlerLeak")
    @Override
    protected void initParams() {
        super.initParams();
        calibrationType = getIntent().getStringExtra("CalibrationType");
        deviceBean = new Gson().fromJson(getIntent().getStringExtra("DeviceBean"), DeviceBean.class);

        bluetoothLeUtil = new BluetoothLeUtil(this);
        bluetoothAdapter = bluetoothLeUtil.getBluetoothAdapter(getActivity());

        if (bluetoothAdapter == null) {
            BluetoothDialogHelper.showDialog(getActivity(), "提示", "当前设备不支持蓝牙", false, new BluetoothDialogHelper.DialogBtnCallback() {
                @Override
                public void dialogBtnCallback(int type) {
                    AppUtils.exitApp();
                }
            });
            return;
        }

        progressDialog = new ProgressDialog(getActivity());
        progressDialog.setMessage("连接中，请稍等......");
        progressDialog.setCancelable(false);
        progressDialog.show();

        bluetoothLeUtil.bindBluetoothService(getActivity(), BluetoothLeService.class, BluetoothLeParams.intentActions);


        handler2 = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {
                    case 0:
                        tvElectric.setText("剩余电量： ".concat(String.valueOf(msg.obj)).concat("%"));
                        break;
                    case 1:
                        tvVersion.setText("固件版本： ".concat(String.valueOf(msg.obj)));
                        break;
                    case 2:
                        oneTvShow1.setText(String.valueOf(msg.obj));
                        inputNum2 = Float.parseFloat(String.valueOf(msg.obj));
                       /* if (materialDialog != null) {
                            materialDialog.title("速度校准 （" + String.valueOf(msg.obj) + "）");
                        }*/
                        if (alertDialog != null && alertDialog.isShowing())
                            alertDialog.setMessage("请改变跑步机速度，等待稳定后输入到下栏中。（当前速度：" + String.valueOf(msg.obj) + "）");
                        break;
                    case 3:
                        oneTvShow2.setText(String.valueOf(msg.obj));
                        break;
                    case 4:
                        twoTv1.setText(String.valueOf(msg.obj));
                        break;
                    case 10:
                        oneBtnConfirm.setEnabled(true);
                        /*materialDialog = new MaterialDialog.Builder(getActivity());
                        materialDialog.title("速度校准");
                        materialDialog.content("请改变跑步机速度，等待稳定后输入到下栏中");
                        materialDialog.inputType(InputType.TYPE_NUMBER_FLAG_SIGNED);
                        materialDialog.cancelable(false);
                        materialDialog.input("请填写速度", null, false, new MaterialDialog.InputCallback() {
                            @Override
                            public void onInput(@NonNull MaterialDialog dialog, CharSequence input) {
                                inputNum = Float.parseFloat(input.toString());
                                ToastUtils.showShort(Math.abs(inputNum - inputNum2) > 0.1 ? "校准失败，重新开始校准" : "校准成功");
                                dialog.dismiss();
                            }
                        });
                        materialDialog.show();*/
                        final EditText et = new EditText(getActivity());
                        et.setHint("请输入速度");
                        et.setInputType(InputType.TYPE_CLASS_NUMBER);

                        alertDialog = new AlertDialog.Builder(getActivity()).setTitle("速度校准")
                                .setMessage("请改变跑步机速度，等待稳定后输入到下栏中。").setView(et).setPositiveButton("确定", null).create();
                        alertDialog.show();
                        alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                if (et.getText() == null || et.getText().toString().trim().equals("")) {
                                    ToastUtils.showShort("请输入速度");
                                    return;
                                }
                                if (et.getText().toString().trim().equals(temp_1)) {
                                    ToastUtils.showShort("请输入其他数值");
                                    return;
                                }
                                inputNum = Float.parseFloat(et.getText().toString().trim());
                                if (Math.abs(inputNum - inputNum2) > 0.1) {
                                    ToastUtils.showShort("校准失败，重新开始校准");
                                } else {
                                    ToastUtils.showShort("校准成功");
                                    new Handler().postDelayed(new Runnable() {
                                        @Override
                                        public void run() {
                                            getActivity().finish();
                                        }
                                    }, 1500);
                                }

                                alertDialog.dismiss();
                            }
                        });
                        break;
                    case 5:
                        switch (String.valueOf(msg.obj)) {
                            case "80AA":
                                state_1 = false;
                                ToastUtils.showShort("设备接收到开始指令");
                                twoBtn1.setEnabled(true);
                                break;
                            case "80BB":
                                state_2 = false;
                                ToastUtils.showShort("设备接收到坡度1指令");
                                twoBtn2.setEnabled(true);
                                break;
                            case "80CC":
                                state_3 = false;
                                ToastUtils.showShort("设备接收到坡度2指令");
                                twoBtn3.setEnabled(true);
                                break;
                            case "80DD":
                                state_4 = false;
                                ToastUtils.showShort("设备接收到坡度3指令");
                                twoBtnOver.setEnabled(true);
                                break;
                            case "80EE":
                                state_5 = false;
                                ToastUtils.showShort("设备接收到校准指令");
                                break;
                        }
                        break;
                    case 20:
                        new AlertDialog.Builder(getActivity()).setTitle("警告").setMessage("设备蓝牙已经断开连接!!!").setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                ActivityUtils.startActivity(getActivity(), IndexActivity.class);
                                getActivity().finish();
                            }
                        }).show();
                        break;
                }
            }
        };
    }

    @Override
    protected int initLayoutId() {
        return R.layout.activity_calibration;
    }

    @Override
    protected void initFvb() {
        super.initFvb();
        ButterKnife.bind(this);
    }

    @Override
    protected void initTitle() {
        super.initTitle();
        TitleBar titleBar = findViewById(R.id.tb_title_bar);
        titleBar.setTitle(calibrationType.equals("ONE") ? "跑步机轮径校准" : "跑步机坡度校准");
        titleBar.setBackgroundColor(Color.BLACK);
        titleBar.setTitleColor(Color.WHITE);
        titleBar.setLeftImageResource(R.mipmap.a2);
        titleBar.setLeftClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                bluetoothLeUtil.closeBleConnectDevice();
                getActivity().finish();
            }
        });

    }


    @Override
    protected void initView() {
        super.initView();
        tvId.setText("设备ID：".concat(deviceBean.getDevice().getName()));
        tlTestOne.setVisibility(calibrationType.equals("ONE") ? View.VISIBLE : View.GONE);
        tlTestTwo.setVisibility(calibrationType.equals("TWO") ? View.VISIBLE : View.GONE);

        twoBtn1.setEnabled(false);
        twoBtn2.setEnabled(false);
        twoBtn3.setEnabled(false);
        twoBtnOver.setEnabled(false);
    }

    @OnClick(R.id.one_btn_confirm)
    public void onOneBtnConfirmClicked() {

/*        final EditText et = new EditText(this);
        et.setHint("请输入速度");
        et.setInputType(InputType.TYPE_CLASS_NUMBER);

        final AlertDialog alertDialog = new AlertDialog.Builder(getActivity()).setTitle("速度校准")
                .setMessage("请改变跑步机速度，等待稳定后输入到下栏中").setView(et).setPositiveButton("确定", null).create();
        alertDialog.show();
        alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (et.getText() == null || et.getText().toString().trim().equals("")) {
                    ToastUtils.showShort("请输入速度");
                    return;
                }
                alertDialog.setTitle(et.getText().toString());
            }
        });*/

        if (!isConnectDevice) {
            ToastUtils.showShort("设备未连接");
            return;
        }

        String strSpeedV1 = oneEtSpeed.getText().toString().trim();
        if (strSpeedV1.equals("")) {
            ToastUtils.showShort("请填写跑步机速度");
            return;
        }
        temp_1 = strSpeedV1;
        if (strSpeedV1.length() == 1) {
            strSpeedV1 = "0".concat(strSpeedV1);
        }
        try {
            bluetoothLeUtil.writeRXCharacteristicUTF_8("88".concat(strSpeedV1));
            tempValue = "88".concat(strSpeedV1);
            oneBtnConfirm.setEnabled(false);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

    @OnClick(R.id.two_btn_1)
    public void onTwoBtn1Clicked() {
        if (!isConnectDevice) {
            ToastUtils.showShort("设备未连接");
            return;
        }
        String value = twoEt1.getText().toString().trim();


        if (!isStartPoDu) {
            ToastUtils.showShort("请点击“开始校准数据”按钮");
            return;
        }

        if (value.equals("")) {
            ToastUtils.showShort("请输入状态1的坡度");
            return;
        }

        temp_11 = value;
        if (value.length() == 1) {
            value = "0".concat(value);
        }
        value = "80".concat(value);
        try {
            bluetoothLeUtil.writeRXCharacteristicUTF_8(value);
            tempValue = "80".concat(value);
            //    writePoDu = 1;
            twoBtn1.setEnabled(false);
            twoIv1.setVisibility(View.VISIBLE);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                if (state_2) {
                    twoBtn1.setEnabled(true);
                    twoIv1.setVisibility(View.INVISIBLE);
                    ToastUtils.showShort("请输入状态1的坡度");
                }
            }
        }, 3000);
    }

    @OnClick(R.id.two_btn_2)
    public void onTwoBtn2Clicked() {
        if (!isConnectDevice) {
            ToastUtils.showShort("设备未连接");
            return;
        }
        String value = twoEt2.getText().toString().trim();

      /*  if (writePoDu != 1) {
            ToastUtils.showShort("请发送状态1数据");
            return;
        }*/

        if (value.equals("")) {
            ToastUtils.showShort("请输入状态2的坡度");
            return;
        }

        temp_12 = value;

        if (Float.parseFloat(temp_12) <= Float.parseFloat(temp_11)) {
            ToastUtils.showShort("输入数值需大于坡度一数值");
            return;
        }

        if (value.length() == 1) {
            value = "0".concat(value);
        }
        value = "80".concat(value);
        try {
            bluetoothLeUtil.writeRXCharacteristicUTF_8(value);
            tempValue = "80".concat(value);
            // writePoDu = 2;
            twoBtn2.setEnabled(false);
            twoIv2.setVisibility(View.VISIBLE);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                if (state_3) {
                    twoBtn2.setEnabled(true);
                    twoIv2.setVisibility(View.INVISIBLE);
                    ToastUtils.showShort("请输入状态2的坡度");
                }
            }
        }, 3000);
    }

    @OnClick(R.id.two_btn_3)
    public void onTwoBtn3Clicked() {
        if (!isConnectDevice) {
            ToastUtils.showShort("设备未连接");
            return;
        }
        String value = twoEt3.getText().toString().trim();

       /* if (writePoDu != 2) {
            ToastUtils.showShort("请发送状态2数据");
            return;
        }*/

        if (value.equals("")) {
            ToastUtils.showShort("请输入状态3的坡度");
            return;
        }

        temp_13 = value;

        if (Float.parseFloat(temp_13) <= Float.parseFloat(temp_12)) {
            ToastUtils.showShort("输入数值需大于坡度二数值");
            return;
        }

        if (value.length() == 1) {
            value = "0".concat(value);
        }
        value = "80".concat(value);
        try {
            bluetoothLeUtil.writeRXCharacteristicUTF_8(value);
            tempValue = "80".concat(value);
            //   writePoDu = 3;
            twoBtn3.setEnabled(false);
            twoIv3.setVisibility(View.VISIBLE);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                if (state_4) {
                    twoBtn3.setEnabled(true);
                    twoIv3.setVisibility(View.INVISIBLE);
                    ToastUtils.showShort("请输入状态3的坡度");
                }
            }
        }, 3000);
    }

    @OnClick(R.id.two_btn_start)
    public void onTwoBtnStartClicked() {
        if (!isConnectDevice) {
            ToastUtils.showShort("设备未连接");
            return;
        }
        try {
            bluetoothLeUtil.writeRXCharacteristicUTF_8("80AA");
            tempValue = "80".concat("AA");
            isStartPoDu = true;
            twoBtnStart.setEnabled(false);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                if (state_1) {
                    twoBtnStart.setEnabled(true);
                    ToastUtils.showShort("请点击“开始校准数据”按钮");
                }
            }
        }, 3000);
    }

    @OnClick(R.id.two_btn_over)
    public void onTwoBtnOverClicked() {
        if (!isConnectDevice) {
            ToastUtils.showShort("设备未连接");
            return;
        }
       /* if (writePoDu != 3) {
            ToastUtils.showShort("请发送状态3数据");
            return;
        }*/
        try {
            bluetoothLeUtil.writeRXCharacteristicUTF_8("80BB");
            tempValue = "80BB";
            isOverPoDu = true;
            twoBtnOver.setEnabled(false);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                if (state_5) {
                    twoBtnOver.setEnabled(true);
                    ToastUtils.showShort("请点击“校准数据输入完毕”按钮");
                }
            }
        }, 3000);
    }

    @Override
    protected void initOnDestroy() {
        bluetoothLeUtil.closeBleConnectDevice();
        super.initOnDestroy();
    }

    @Override
    public void onBluetoothUtilCallback(String utilCallbackStatue, BluetoothGatt gatt, BluetoothDevice device, int rssi, byte[] scanRecord, IBinder rawBinder, final Intent intent) {
        if (!utilCallbackStatue.equals(STATUS_BluetoothDevice_Get_Data))
            LogUtils.i("UtilCallbackStatue", utilCallbackStatue);
        switch (utilCallbackStatue) {
            case BluetoothLeParams.UtilCallbackStatue.STATUS_ServiceConnection_ServiceConnected:
                bluetoothLeService = ((BluetoothLeService.LocalBinder) rawBinder).getService(bluetoothLeUtil, bluetoothAdapter);
                bluetoothLeUtil.openBleConnectDevice(bluetoothLeService, deviceBean.getDevice());
                break;
            case BluetoothLeParams.UtilCallbackStatue.STATUS_ServiceConnection_ServiceDisconnected:
                ToastUtils.showLong("服务失去连接");
                break;
            case BluetoothLeParams.UtilCallbackStatue.STATUS_BluetoothDevice_Connecting:
                progressDialog.show();
                break;
            case BluetoothLeParams.UtilCallbackStatue.STATUS_BluetoothDevice_Connected:
                isConnectDevice = true;
                if (progressDialog.isShowing()) progressDialog.dismiss();
                handler.postDelayed(runnable, TIME);
                break;
            case BluetoothLeParams.UtilCallbackStatue.STATUS_BluetoothDevice_DisConnecting:
                isConnectDevice = false;
                if (progressDialog.isShowing()) progressDialog.dismiss();
                // 在初始化方法里.
                break;
            case BluetoothLeParams.UtilCallbackStatue.STATUS_BluetoothDevice_DisConnected:
                isConnectDevice = false;
                if (progressDialog.isShowing()) progressDialog.dismiss();
                tempSendMessage(20, "");
                break;
            case BluetoothLeParams.UtilCallbackStatue.STATUS_BluetoothDevice_Connect_Fail:
                ToastUtils.showShort("当前设备不能连接");
                break;
            case BluetoothLeParams.UtilCallbackStatue.STATUS_BluetoothDevice_Discovered:
                break;
            case BluetoothLeParams.UtilCallbackStatue.STATUS_BluetoothDevice_Discovered_Fail:
                LogUtils.i("STATUS_BluetoothDevice_Discovered_Fail");
                break;
            case BluetoothLeParams.UtilCallbackStatue.STATUS_BluetoothDevice_Get_Data:
                byte[] txValue = intent.getByteArrayExtra(EXTRA_DATA);
                String text = null;
                try {
                    text = new String(txValue, "UTF-8");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                LogUtils.i("ReceiveData", text);
                if (calibrationType.equals("ONE") && text != null) {
                    if (text.equals("88COM")) {
                        tempSendMessage(10, "设备校准完成，可继续输入速度信息");
                        return;
                    }
                    if (text.equals("88GG")) {
                        LogUtils.i("设备接收到速度信息");
                    } else if (text.startsWith("88")) {
                        String strRotationRate = text.substring(2);
                        if (strRotationRate.startsWith("0"))
                            strRotationRate.substring(1);
                        if (!strRotationRate.contains("."))
                            strRotationRate = strRotationRate + ".0";
                        tempSendMessage(2, strRotationRate);
                    } else if (text.startsWith("89")) {
                        String strRotationRate = text.substring(2);
                        if (strRotationRate.startsWith("0"))
                            strRotationRate.substring(1);
                        tempSendMessage(3, strRotationRate);
                    }

                } else if (calibrationType.equals("TWO") && text != null) {

                    if (text.equals("80GG") || text.equals("80AA") || text.equals("80BB") || text.equals("80CC") || text.equals("80DD") || text.equals("80EE")) {
                        tempSendMessage(5, text);
                    } else if (text.startsWith("80")) {
                        String temp = text.substring(2);
                        if (temp.startsWith("0"))
                            temp.substring(1);
                        tempSendMessage(4, temp.concat("%"));
                    }
                }
                break;
            case BluetoothLeParams.UtilCallbackStatue.STATUS_BluetoothDevice_Get_BatteryData:
                byte[] txValue2 = intent.getByteArrayExtra(EXTRA_DATA);
                final StringBuilder text2 = new StringBuilder();
                for (byte b : txValue2) {
                    String a = String.valueOf(b);
                    text2.append(a);
                }
                tempSendMessage(0, String.valueOf(text2));
                break;
            case BluetoothLeParams.UtilCallbackStatue.STATUS_BluetoothDevice_Get_VersionData:
                byte[] txValue3 = intent.getByteArrayExtra(BLeUtil.Params.EXTRA_DATA);
                String text3 = "";
                try {
                    text3 = new String(txValue3, "UTF-8");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }

                tempSendMessage(1, text3);
                break;
        }
    }


    private void tempSendMessage(int what, String message) {
        Message message1 = new Message();
        message1.what = what;
        message1.obj = message;
        handler2.sendMessage(message1);
    }
}
